<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org" xmlns:v-on="http://www.w3.org/1999/xhtml"
      xmlns:v-if="http://www.w3.org/1999/xhtml">
<head th:replace="~{baseui/head :: head}">
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="description" content="动检通智慧云平台"/>
    <meta name="author" content="牧中(上海)物联网科技有限公司"/>
    <link rel="shortcut icon" href="/webjars/images/favicon.png"/>

    <title>牧中畜牧业大数据智慧云平台V1.0</title>


</head>
<body>
<!--顶部一级导航-->
<th:block th:replace="baseui/topheader :: topheader(1)"/>

<!--动检通菜单-->
<div id="topnav" th:replace="djt/partial/topnav :: topnav(4)"></div>


<div id="cl-wrapper">
    <!--左边导航-->
    <th:block th:replace="djt/partial/inspector_left :: nav(2)"/>

    <div class="container-fluid" id="pcont">
        <div class="cl-mcont">
            <div class="row">
                <div class="col-md-12">
                    <div class="block-flat">
                        <div id="show" class="header from-group"><h3>督查记录</h3>
                        </div>

                        <div class="content">
                            <div class="table-responsive">
                                <div id="datatable_wrapper"
                                     class="dataTables_wrapper form-inline dt-bootstrap no-footer">
                                    <div class="row" >

                                        <div class="from-group">
                                            <div id="but" class="btn-group col-sm-2" style="width: 180px;float: left">
                                                <button date-id="but1" type="button"
                                                        class="btn btn-default btn-sm"
                                                        onclick="SimpleSearch(1)">
                                                    日
                                                </button>
                                                <button date-id="but2" type="button" class="btn btn-default btn-sm"
                                                        onclick="SimpleSearch(2)">
                                                    周
                                                </button>
                                                <button date-id="but3" type="button" class="btn btn-default btn-sm"
                                                        onclick="SimpleSearch(3)">
                                                    月
                                                </button>
                                                <button date-id="but4" type="button" class="btn btn-default btn-sm"
                                                        onclick="SimpleSearch(4)">
                                                    季
                                                </button>
                                                <button date-id="but6" type="button" class="btn btn-default btn-sm"
                                                        onclick="SimpleSearch(6)">
                                                    年
                                                </button>
                                            </div>

                                            <div class="col-sm-2">
                                                <div class="input-group date datetime col-md-5 col-xs-7"
                                                     data-min-view="2" data-date-format="yyyy-mm-dd">
                                                    <input id="start" class="form-control" style="width: 90px;"
                                                           size="16"
                                                           type="text" value="" placeholder="开始时间" readonly>
                                                    <span class="input-group-addon btn btn-primary"><span
                                                            class="glyphicon glyphicon-th"></span></span>
                                                </div>
                                            </div>
                                            <div class="col-sm-2">
                                                <div class="input-group date datetime col-md-5 col-xs-7"
                                                     data-min-view="2" data-date-format="yyyy-mm-dd">
                                                    <input id="end" class="form-control" style="width: 90px;" size="16"
                                                           type="text" value="" placeholder="结束时间" readonly>
                                                    <span class="input-group-addon btn btn-primary"><span
                                                            class="glyphicon glyphicon-th"></span></span>
                                                </div>


                                            </div>

                                            <!--<option value="">养殖场名称</option>
                                            <option th:each=" breedfactory : ${factoryname}" th:text="${breedfactory.name}"
                                                    th:value="${breedfactory.name}"></option>-->
                                            <div class="col-sm-2" style="width: 150px">
                                                <select id="area" class="select2"  onchange="showfactory()">
                                                    <option value="全部">所有区域</option>
                                                    <optgroup th:each="cityarea:${areaList}"
                                                              th:label="${cityarea.city}">
                                                        <option th:each="regions:${cityarea.regions}"
                                                                th:text="${regions}" th:value="${regions}"></option>
                                                    </optgroup>

                                                </select>
                                            </div>

                                            <div class="col-sm-2" style="width: 150px">
                                                <!-- 1全部 2待处理 3待确认 4已确认-->
                                                <select id="status" class="select2">
                                                    <option value="-5">状态</option>
                                                    <option value="2">待处理</option>
                                                    <option value="3">待确认</option>
                                                    <option value="4">已确认</option>
                                                </select>

                                            </div>


                                        </div>
                                    </div>
                                    <div class="row" style="margin-left: 175px">
                                        <div class="from-group">
                                            <div class="col-sm-2" style="width: 150px">
                                                <select id="type" class="select2">
                                                    <option value="0">动物种类</option>
                                                    <option value="1">猪</option>
                                                    <option value="2">牛</option>
                                                    <option value="3">羊</option>
                                                    <option value="4">家禽</option>

                                                </select>

                                            </div>
                                            <div class="col-sm-2" style="width: 150px">
                                                <select id="name" class="select2">
                                                    <option value="">选择督查人</option>
                                                    <option th:each="name:${nameList}"
                                                            th:text="${name}" th:value="${name}"></option>

                                                </select>
                                            </div>
                                            <div class="col-sm-2" style="width: 150px">
                                                <select id="breedfac" class="select2">
                                                    <option value="-1">选择养殖场</option>
                                                    <span th:each=" breedFactory :${facList}">
                                                                  <option th:text="${breedFactory.name}"
                                                                          th:value="${breedFactory.id}">选项 1</option>

                                                                   </span>

                                                </select>
                                            </div>
                                            <div class="col-sm-2">
                                                <button class="btn btn-primary" onclick="SearchBtn()">搜索</button>
                                            </div>
                                        </div>

                                    </div>

                                    <br/>

                                    <div class="row">
                                        <div class="col-sm-12">

                                            <!-- Table -->
                                            <table id="jqGrid"></table>
                                            <div id="jqGridPager"></div>

                                        </div>
                                    </div>


                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>


        </div>
    </div>

</div>

<script type="text/javascript" src="/webjars/js/jquery.js"></script>
<script type="text/javascript" src="/webjars/js/jquery.gritter/js/jquery.gritter.js"></script>

<script type="text/javascript" src="/webjars/js/jquery.nanoscroller/jquery.nanoscroller.js"></script>
<script type="text/javascript" src="/webjars/js/behaviour/general.js"></script>
<script src="/webjars/js/jquery.ui/jquery-ui.js" type="text/javascript"></script>

<script type="text/javascript" src="/webjars/js/jquery.nestable/jquery.nestable.js"></script>
<script type="text/javascript" src="/webjars/js/bootstrap.switch/bootstrap-switch.min.js"></script>
<script type="text/javascript" src="/webjars/js/bootstrap.datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
<script src="/webjars/js/jquery.select2/select2.min.js" type="text/javascript"></script>
<script src="/webjars/js/skycons/skycons.js" type="text/javascript"></script>
<script src="/webjars/js/bootstrap.slider/js/bootstrap-slider.js" type="text/javascript"></script>
<script type="text/javascript" src="/webjars/js/jquery.niftymodals/js/jquery.modalEffects.js"></script>
<script type="text/javascript" src="/webjars/js/bootstrap.summernote/dist/summernote.min.js"></script>


<script type="text/javascript" src="/webjars/js/jquery.magnific-popup/dist/jquery.magnific-popup.min.js"></script>


<script src="/webjars/js/behaviour/voice-commands.js"></script>
<script src="/webjars/js/bootstrap/dist/js/bootstrap.min.js"></script>

<script type="text/ecmascript" src="/webjars/jqgrid/js/i18n/grid.locale-cn.js"></script>
<script type="text/ecmascript" src="/webjars/jqgrid/js/jquery.jqGrid.min.js"></script>
<script src="/webjars/js/laydate/laydate.js"></script>
<script src="//vuejs.org/js/vue.min.js"></script>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
<script src="/webjars/js/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript">
    var PostDatas = {area:city()};
    $(document).ready(function () {

        App.init();


        //grid
        $("#jqGrid").jqGrid({
            url: '/djt/inspector/recordsindex',
            mtype: "GET",
            postData: PostDatas,
            styleUI: 'Bootstrap',
            datatype: "json",
            autowidth: true,
            colModel: [
                {
                    label: '序号',
                    name: 'supervisionRecords.id',
                    key: true,
                    width: 50,
                    align: 'center',
                    formatter: showid
                },
                {label: '区域', name: 'breedFactory.region', width: 75, align: 'center'},
                {label: '养殖场名称', name: 'supervisionRecords.breedFactoryName', width: 100, align: 'center'},
                {label: '业主姓名', name: 'supervisionRecords.ownerName', width: 75, align: 'center'},
                {
                    label: '养殖品种',
                    name: 'supervisionRecords.breedType',
                    width: 80,
                    align: 'center',
                    formatter: showtype
                },
                {
                    label: '存栏数',
                    name: 'supervisionRecords.stockQuantity',
                    width: 100,
                    align: 'center'

                },
                {label: '督查人', name: 'supervisionRecords.userName', width: 75, align: 'center'},
                {label: '状态', name: 'supervisionRecords.status', width: 80, align: 'center', formatter: showstatus},
                {label: '日期', name: 'supervisionRecords.createdAt', width: 120, align: 'center', formatter: ShowDate},
                {label: '详细', width: 150, formatter: ShowOpertator, align: 'center'}
            ],
            viewrecords: true,
            height: 350,
            rowNum: 20,
            pager: "#jqGridPager"
        });

    });

    function showfactory() {



        $.ajax({

            url: "/djt/breedarch/search",
            data: {region: city(), breedType: -1},
            dataType: "json",
            success: function (data) {

                $("#breedfac").empty();

                if (data.length != 0) {
                    var str1 = " <option value='-1' selected>所有养殖场</option>";
                    $("#breedfac").append(str1);
                    $.each(data, function (index, obj) {
                        var id = obj.id;
                        var name = obj.name;
                        var str = "<option value=" + id + ">" + name + "</option>";

                        $("#breedfac").append(str);
                    });
                } else {

                    var str = "<option value='0' selected>无</option>";
                    $("#breedfac").append(str);
                }
            }
        });
    }

    function ShowDate(cellvalue, options, rowObject) {
        if (cellvalue != null && cellvalue != "") {
            var timestamp4 = new Date(cellvalue);
            var dt = timestamp4.toLocaleDateString().replace(/\//g, "-") + " " + timestamp4.toTimeString().substr(0, 8);
            return dt;
        } else {
            return "";
        }
    }

    /* var start = {
     elem: '#start',
     format: 'YYYY/MM/DD hh:mm:ss',

     max: '2099-06-16 23:59:59', //最大日期
     istime: true,
     istoday: false,
     choose: function (datas) {
     end.min = datas; //开始日选好后，重置结束日的最小日期
     end.start = datas //将结束日的初始值设定为开始日
     }
     };
     var end = {
     elem: '#end',
     format: 'YYYY/MM/DD hh:mm:ss',
     max: '2099-06-16 23:59:59',
     istime: true,
     istoday: false,
     choose: function (datas) {
     start.max = datas; //结束日选好后，重置开始日的最大日期
     }
     };
     laydate(start);
     laydate(end);*/

    /*    function change(num) {
     $("#hiddate").val(num);
     $("button[date-id = but" + num + "]").addClass("active");
     $("button[date-id = but" + num + "]").siblings().removeClass("active");
     }*/
    function city() {

        var city = $("#area").val();


        if (city =="全部") {
            var array1 = new Array();
            $("#area option").each(function () {  //遍历所有option
                var txt = $(this).val();   //获取option值
                if (txt != '') {
                    array1.push(txt);  //添加到数组中
                }
            });

            return array1.toString();
        }else {
            var array2 = new Array();
            array2.push(city);
            return array2.toString();
        }

    }
    function SearchBtn() {
        var time1 = $("#start").val();
        var time2 = $("#end").val();
        var area = city();
        var type = $("#type").val();
        var status = $("#status").val();
        var name = $("#name").val();
        var breedfac = $("#breedfac").val();
        if (time1 == "") {
            time1 = new Date("2000-01-01 :00:00:00");
        } else {
            time1 = new Date(time1);
        }
        if (time2 == "") {
            time2 = new Date();
        } else {
            time2 = new Date(time2);
        }
        if (area == "") {
            area = null;
        }
        if (type == "") {
            type = 0;
        }
        if (status == "") {
            status = -5;
        }

        var sdata = {   // 构建查询需要的参数
            start: time1,
            end: time2,
            area: area,
            type: type,
            status: status,
            name: name,
            breedfac: breedfac
        };

        var postData = $("#jqGrid").jqGrid("getGridParam", "postData");

        $.extend(postData, sdata);

        $("#jqGrid").jqGrid("setGridParam", {
            search: true    // (6)将jqGrid的search选项设为true
        }).trigger("reloadGrid", [{page: 1}]);   // (7)重新载入Grid表格，以使上述设置生效

    }

    var SimpleSearch = function (num) {
        var end = new Date();
        var start = new Date();
        switch (num) {
            case 1 :
                start = new Date(new Date() - 24 * 60 * 60 * 1000);
                break;
            case 2:
                start = new Date(new Date() - 7 * 24 * 60 * 60 * 1000);
                break;
            case 3:
                start = new Date(new Date() - 30 * 24 * 60 * 60 * 1000);
                break;
            case 4:
                start = new Date(new Date() - 4 * 30 * 24 * 60 * 60 * 1000);
                break;
            case 5:
                start = new Date(new Date() - 6 * 30 * 24 * 60 * 60 * 1000);
                break;
            case 6:
                start = new Date(new Date() - 12 * 30 * 24 * 60 * 60 * 1000);
                break;
        }
        var sdata = {   // 构建查询需要的参数
            start: start,
            end: end,
            area: $("#area").val(),
            type: $("#type").val(),
            status: $("#status").val(),
            name: $("#name").val(),
            breedfac: $("#breedfac").val()
        };

        var postData = $("#jqGrid").jqGrid("getGridParam", "postData");

        $.extend(postData, sdata);

        $("#jqGrid").jqGrid("setGridParam", {
            search: true    // (6)将jqGrid的search选项设为true
        }).trigger("reloadGrid", [{page: 1}]);   // (7)重新载入Grid表格，以使上述设置生效

    }

    function showid(cellvalue, options, rowObject) {
        return cellvalue == -1 ? "合计" : cellvalue;

    }
    function showtype(cellvalue, options, rowObject) {
        /*1、猪 2、牛 3、羊 4、家禽*/
        switch (cellvalue) {
            case 1:
                return "猪";
                break;
            case 2:
                return "牛";
                break;
            case 3:
                return "羊";
                break;
            case 4:
                return "家禽";
                break;
            default :
                return "";

        }
    }

    /*2待处理 3待确认 4已确认*/

    function showstatus(cellvalue, options, rowObject) {
        switch (Number(cellvalue)) {

            case 2:
                return "待处理";
                break;
            case 3:
                return "待确认";
                break;
            case 4:
                return "已确认";
                break;
        }

    }
    function ShowOpertator(cellvalue, options, rowObject) {
        var link;
        var row = JSON.stringify(rowObject);

        link = "<button type='button' class='btn btn-default' id='" + row + "' onclick='select(this.id)'>查看</button>";
        return rowObject.id == -1 ? "" : link;
    }

    function select(data) {
        var json = JSON.parse(data);
        var type = showtype(json.supervisionRecords.breedType, null, null);
        var timestamp4 = new Date(json.supervisionRecords.createdAt);
        var dt = timestamp4.toLocaleDateString().replace(/\//g, "-") + " " + timestamp4.toTimeString().substr(0, 8);
        layui.use(['laypage', 'layer'], function () {
            var $ = layui.jquery, layer = layui.layer;
            layer.open({
                type: 2,
                skin: 'layui-layer-lan',
                area: ['800px', '650px'],
                content: ['/djt/inspector/supervisionDetail', 'no'],
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']];

                    body.find("#factory").text(json.breedFactory.name);
                    body.find("#factorynum").text(json.breedFactory.id);
                    body.find("#region").text(json.breedFactory.region);
                    body.find("#stable").text(json.breedFactory.stableNumber);
                    body.find("#userName").text(json.supervisionRecords.userName);
                    body.find("#date").text(dt);
                    body.find("#protype").text(type);
                    body.find("#num").text(json.breedFactory.stockQuantity);
                    body.find("#safetyDetection").text(json.supervisionRecords.safetyDetection);
                    body.find("#animalCheckExecutionDetails").text(json.supervisionRecords.animalCheckExecutionDetails);
                    body.find("#harmlessTreatmentDetails").text(json.supervisionRecords.harmlessTreatmentDetails);
                    body.find("#inputUsedDetails").text(json.supervisionRecords.inputUsedDetails);
                    body.find("#pollutionControlDetails").text(json.supervisionRecords.pollutionControlDetails);
                    body.find("#correctSuggestion").text(json.supervisionRecords.correctSuggestion);



                    var tt=json.supervisionRecords.photoMap;


                    for (var key in tt){
                        if (tt[key]!=null){
                            var a = "<img src="+tt[key]['uri']+" class='img-thumbnail' layer-src="+tt[key]['uri']+" style='width: 150px;height: 200px' onclick='showimg(this)'>";
                            if (tt[key]['uri']!=null&&tt[key]['uri']!=""){
                                body.find("#photosJson").append(a);
                            }
                        }


                    }



                }
            });

        });
    }
    $("#area").change(function () {
        $.ajax({
            type: "GET",
            url: "/djt/inspector/breedByRegion",
            data:{region:$("#area").val()},
            dataType: "json",
            success: function (data) {
                $("#breedfac").html("");
                $("#breedfac").append("<option value='0'>选择养殖场</option>");
                var str = "";
                /*<option value='0'>选择养殖场</option>*/
                $.each(data, function (index, item) {
                    str += "<option value="+item+">"+item+"</option>";
                    $("#breedfac").append(str);
                    str = "";
                })
            }
        })

    })
</script>


</body>
</html>
